package team.bluepen.supermarket.data.entity;

import team.bluepen.supermarket.data.dao.abstraction.Buildable;
import team.bluepen.supermarket.util.StringUtil;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.Objects;

/**
 * 商品对象
 * @author Kai
 */
@SuppressWarnings("unused")
public class Good implements Serializable, HbaseItem<Good>, Buildable<Good> {
    /** Hbase中存储的Row ID */
    private @Nonnull String row;

    /** 商品ID */
    private long id;

    /** 商品名称 */
    private String name;

    /** 商品数量 */
    private int amount;

    /** 商品价格 */
    private int price;

    /** 商品状态，上架或下架。<br> 对应状态：
     * <ul>
     *     <li> U（上架）-> true</li>
     *     <li> D（下架）-> false</li>
     * </ul>
     */
    private boolean state;

    public Good() {
        row = "";
    }

    public Good(@Nonnull String row, long id, String name, int amount, int price, boolean state) {
        this.row = row;
        this.id = id;
        this.name = name;
        this.amount = amount;
        this.price = price;
        this.state = state;
    }

    public long getId() {
        return id;
    }

    public Good setId(long id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public Good setName(String name) {
        this.name = name;
        return this;
    }

    public int getAmount() {
        return amount;
    }

    public Good setAmount(int amount) {
        this.amount = amount;
        return this;
    }

    public int getPrice() {
        return price;
    }

    public Good setPrice(int price) {
        this.price = price;
        return this;
    }

    public boolean getBooleanProductState() {
        return state;
    }

    public String getState() {
        if (state) {
            return "U";
        }
        return "D";
    }

    @Nonnull
    public String getRow() {
        return row;
    }

    @Override
    public Good generateRowKey() {
        String rowKey = "";
        if (check()) {
            rowKey = StringUtil.convert(id, 7);
        }
        row = rowKey;
        return this;
    }

    private boolean check() {
        return id != 0 && name != null;
    }

    public Good setRow(@Nonnull String row) {
        this.row = row;
        return this;
    }

    public Good setState(boolean state) {
        this.state = state;
        return this;
    }

    public Good setState(char productState) {
        if (productState == 'U' || productState == 'u') {
            this.state = true;
            return this;
        }
        if (productState == 'D' || productState == 'd'){
            this.state = false;
        }
        return this;
    }

    public Good setState(String productState) {
        if (StringUtil.isEmpty(productState)){
            return this;
        }
        if ("U".equalsIgnoreCase(productState)){
            this.state = true;
            return this;
        }
        if ("D".equalsIgnoreCase(productState)){
            this.state = false;
            return this;
        }
        if ("true".equalsIgnoreCase(productState)){
            this.state = true;
            return this;
        }
        if ("false".equalsIgnoreCase(productState)){
            this.state = false;
        }
        return this;
    }

    @Override
    public String toString() {
        return "Good{" +
                "row='" + row + '\'' +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", amount=" + amount +
                ", price=" + price +
                ", productState=" + state +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Good good = (Good) o;
        return id == good.id && amount == good.amount
                && price == good.price && state == good.state
                && row.equals(good.row) && Objects.equals(name, good.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, id, name, amount, price, state);
    }

    @Override
    public Good build() {
        return this;
    }
}
